﻿using System;
using System.Data;
using System.Data.SQLite;
using System.Runtime.InteropServices;

namespace AZMapDBHelperPlugin
{
    public static class SQLiteHelper
    {
        private const string CONTAIN_TABLE_SQL = "SELECT count(*) FROM sqlite_master WHERE name=@TableName;";
        public static readonly string DB_PASSWORD;
        private const string DROP_TABLE_SQL = "DROP TABLE {0};";
        private const string DROP_VIEW_SQL = "DROP VIEW {0};";
        private static readonly int SQLITE_PAGE_SIZE;

        static SQLiteHelper()
        {
            DB_PASSWORD = EncryptString.ThisIsLegalString("jztU+R63Ke4=");
            SQLITE_PAGE_SIZE = 0x8000;
        }

        public static void CloseConnection(SQLiteConnection conn)
        {
            try
            {
                conn.Close();
            }
            catch (Exception)
            {
            }
        }

        public static bool ContainTable(SQLiteConnection conn, string tablename)
        {
            object obj2 = ExecuteScalar(conn, "SELECT count(*) FROM sqlite_master WHERE name=@TableName;", new SQLiteParameter[] { new SQLiteParameter("@TableName", tablename) });
            if (obj2 == null)
            {
                return false;
            }
            return (int.Parse(obj2.ToString()) > 0);
        }

        public static bool DropTable(SQLiteConnection conn, string tableName)
        {
            return ExcuteNoneQueryImmediately(conn, string.Format("DROP TABLE {0};", tableName));
        }

        public static bool DropView(SQLiteConnection conn, string viewName)
        {
            return ExcuteNoneQueryImmediately(conn, string.Format("DROP VIEW {0};", viewName));
        }

        public static bool ExcuteNoneQuery(SQLiteConnection conn, string sql, SQLiteParameter[] parameters = null, SQLiteTransaction transAction = null)
        {
            bool flag;
            try
            {
                using (SQLiteCommand command = new SQLiteCommand(sql, conn))
                {
                    if (transAction != null)
                    {
                        command.Transaction = transAction;
                    }
                    SetParameters(command.Parameters, parameters);
                    flag = command.ExecuteNonQuery() >= 0;
                }
            }
            catch (Exception)
            {
                flag = false;
            }
            return flag;
        }

        public static bool ExcuteNoneQueryImmediately(SQLiteConnection conn, string sql)
        {
            bool flag;
            try
            {
                using (SQLiteCommand command = new SQLiteCommand(sql, conn))
                {
                    flag = command.ExecuteNonQuery() >= 0;
                }
            }
            catch (Exception)
            {
                flag = false;
            }
            return flag;
        }

        public static DataTable ExecuteDataTable(SQLiteConnection conn, string sql, SQLiteParameter[] parameters = null)
        {
            DataTable table;
            try
            {
                using (SQLiteCommand command = new SQLiteCommand(sql, conn))
                {
                    SetParameters(command.Parameters, parameters);
                    using (SQLiteDataAdapter adapter = new SQLiteDataAdapter(command))
                    {
                        using (DataSet set = new DataSet())
                        {
                            adapter.Fill(set);
                            table = set.Tables[0];
                        }
                    }
                }
            }
            catch (Exception)
            {
                table = null;
            }
            return table;
        }

        public static SQLiteDataReader ExecuteReader(SQLiteConnection conn, string sql)
        {
            SQLiteDataReader reader2;
            try
            {
                using (SQLiteCommand command = new SQLiteCommand(sql, conn))
                {
                    reader2 = command.ExecuteReader();
                }
            }
            catch (Exception)
            {
                reader2 = null;
            }
            return reader2;
        }

        public static object ExecuteScalar(SQLiteConnection conn, string sql, SQLiteParameter[] parameters = null)
        {
            object obj2;
            try
            {
                using (SQLiteCommand command = new SQLiteCommand(sql, conn))
                {
                    SetParameters(command.Parameters, parameters);
                    obj2 = command.ExecuteScalar();
                }
            }
            catch (Exception)
            {
                obj2 = null;
            }
            return obj2;
        }

        public static SQLiteConnection GetSQLiteConn(string dataSource)
        {
            SQLiteConnection connection = null;
            SQLiteConnection connection2 = null;
            try
            {
                if ((connection != null) && connection.DataSource.Equals(dataSource))
                {
                    return connection;
                }
                SQLiteConnectionStringBuilder builder = new SQLiteConnectionStringBuilder
                {
                    PageSize = SQLITE_PAGE_SIZE,
                    DataSource = dataSource,
                    Pooling = true
                };
                connection = new SQLiteConnection
                {
                    ConnectionString = builder.ToString()
                };
                connection.SetPassword(DB_PASSWORD);
                connection.Open();
            }
            catch (Exception)
            {
                connection2 = null;
            }
            return connection2;
        }

        public static void SetParameters(SQLiteParameterCollection collection, SQLiteParameter[] parameters = null)
        {
            if (parameters != null)
            {
                foreach (SQLiteParameter parameter in parameters)
                {
                    collection.Add(parameter);
                }
            }
        }
    }
}
